package net.xisberto.phonetodesktop;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.Menu;
import android.support.v4.view.MenuItem;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.MenuInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.dropbox.client2.session.AccessTokenPair;

public class PhoneToDesktopActivity extends FragmentActivity implements OnClickListener {
	private DropboxSynced dropbox;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        TextView tvLinkSite = (TextView) findViewById(R.id.textViewLinkSite);
        tvLinkSite.setText(Html.fromHtml(getResources().getString(R.string.btn_go_to_dropbox_site)));
        tvLinkSite.setMovementMethod(LinkMovementMethod.getInstance());
        TextView tvLinkApp = (TextView) findViewById(R.id.textViewLinkApp);
        tvLinkApp.setText(Html.fromHtml(getResources().getString(R.string.btn_go_to_dropbox_app)));
        tvLinkApp.setMovementMethod(LinkMovementMethod.getInstance());
        
        findViewById(R.id.btn_authorize).setOnClickListener(this);
        findViewById(R.id.btn_unauthorize).setOnClickListener(this);

        dropbox = new DropboxSynced(getApplicationContext());
    }

	@Override
	protected void onResume() {
		super.onResume();
		
		if (dropbox.mDBApi.getSession().authenticationSuccessful()) {
	        try {
	            // MANDATORY call to complete auth.
	            // Sets the access token on the session
	            dropbox.mDBApi.getSession().finishAuthentication();
	            AccessTokenPair token = dropbox.mDBApi.getSession().getAccessTokenPair();
	            dropbox.storeKeys(token.key, token.secret);
	        } catch (IllegalStateException e) {
	            Log.i("DbAuthLog", "Error authenticating", e);
	            dropbox.removeKeys();
	        }
	    }
		updateMainLayout();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater menu_inflater = getMenuInflater();
		menu_inflater.inflate(R.menu.main_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menuitem_link_list:
			Intent intent = new Intent(getApplicationContext(), LinkListActivity.class);
			startActivity(intent);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_authorize:
			dropbox.mDBApi.getSession().startAuthentication(PhoneToDesktopActivity.this);	
			break;
		case R.id.btn_unauthorize:
			dropbox.mDBApi.getSession().unlink();
			dropbox.removeKeys();
			updateMainLayout();
			break;
		default:
			break;
		}
	}
    
    private void updateMainLayout() {
        if (dropbox.isAuthorized()) {
        	findViewById(R.id.linearLayoutAlreadyAuthorized).setVisibility(View.VISIBLE);
        	findViewById(R.id.linearLayoutNeedAuthorize).setVisibility(View.GONE);
        } else {
        	findViewById(R.id.linearLayoutAlreadyAuthorized).setVisibility(View.GONE);
        	findViewById(R.id.linearLayoutNeedAuthorize).setVisibility(View.VISIBLE);
        }
    }

   
}